package com.douziit.meilianoa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douziit.meilianoa.base.annotation.NoCheckToken;
import com.douziit.meilianoa.base.enums.ResponseCode;
import com.douziit.meilianoa.base.request.PageRequest;
import com.douziit.meilianoa.base.response.ResponseData;
import com.douziit.meilianoa.base.response.ResponseUtil;
import com.douziit.meilianoa.base.utils.file.UploadDocumentUtil;
import com.douziit.meilianoa.entity.VersionInfo;
import com.douziit.meilianoa.service.VersionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 版本控制类
 */

@Slf4j
@RestController
@RequestMapping("/version")
public class VersionInfoController {

    @Value("${app.file.path}")
    private String APP_FILE_PATH;

    @Autowired
    private VersionInfoService versionInfoService;

    /**
     * 版本列表
     */
    @PostMapping("/listVersion")
    public ResponseData listVersion() {

        List<VersionInfo> versionInfos = versionInfoService.list();
        return ResponseUtil.jsonSuccess(versionInfos);
    }

    /**
     * 删除版本
     */
    @PostMapping("/deleteVersion")
    public ResponseData deleteVersion(@RequestBody PageRequest req) {

        versionInfoService.removeById(req.getVersionId());
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 添加app版本,上传apk到服务器
     *
     * @param file
     * @return
     */
    @NoCheckToken
    @PostMapping("/uploadApk")
    public ResponseData addAppVersion(@RequestParam MultipartFile file) {
        log.info("uploadAppFile is start>>>>>>>>>>file size:{}", file.getSize());
        String uploadPath = "";
        try {
            uploadPath = UploadDocumentUtil.uploadFile(file.getInputStream(), file.getOriginalFilename(), APP_FILE_PATH);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseUtil.jsonFail(ResponseCode.FILE_UPLOAD_ERROR);
        }
        return ResponseUtil.jsonSuccess(uploadPath);
    }

    /**
     * 版本信息
     *
     * @return
     */
    @PostMapping("/appVersionDetail/{id}")
    public ResponseData appVersionDetail(@PathVariable("id") Integer id) {
        VersionInfo versionInfo = versionInfoService.getOne(new QueryWrapper<VersionInfo>().eq("id",id).orderByDesc("add_time"));
        return ResponseUtil.jsonSuccess(versionInfo);
    }


    /**
     * 添加版本
     *
     * @return
     */
    @PostMapping("/addVersion")
    public ResponseData appVersionList(@RequestBody VersionInfo versionInfo) {
        versionInfo.setAddTime(new Date());
        versionInfoService.save(versionInfo);
        return ResponseUtil.jsonSuccess(0);
    }

    public static void main(String[] args) {

    }

}
